package com.mai.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mai.cart.Cart;
import com.mai.entity.Product;
import com.mai.service.ProductService;

@Controller
public class CartController {
	
	@Autowired
	private ProductService ps;
	
	@ResponseBody
	@RequestMapping("/addCart")
	public String addCart(int product_id,HttpServletRequest request) {
		System.out.println(product_id+"cart的id是");
		Product p = ps.selectProductById(product_id);
		System.out.println("addcart值"+p);
		Cart c = (Cart)request.getSession().getAttribute("cart");
		if(c == null) {
			c = new Cart();
		}
		c.add(p);
		request.getSession().setAttribute("cart", c);
		return "success";
	}
	
	@RequestMapping("/deleteCart")
	public String deleteCart(int product_id,HttpServletRequest request) {
		Cart c = (Cart)request.getSession().getAttribute("cart");
		c.getMap().remove(product_id);
		return "forward:showCart";
	}
	
	@RequestMapping("/showCart")
	public String showCart() {
		return "ShoppingCart";
	}
	
}
